<script lang="ts" setup>
import { ref, reactive, computed } from "vue";
import { FormRules, FormInstance } from "element-plus";
import { DataDictionaryApi } from "@/Api";

interface Props {
   modelValue: boolean;
   dicId?: string;
   value?: DataDictionary.Value;
}

const props = withDefaults(defineProps<Props>(), {
   modelValue: false
});

const emit = defineEmits<{
   (e: "update:modelValue", value: boolean): void;
}>();

const dialogVisible = computed({
   get: () => props.modelValue,
   set: value => {
      if (!value) formRef.value?.resetFields();
      emit("update:modelValue", value);
   }
});
const dicId = computed(() => props.dicId);
const value = computed(() => props.value);

const loading = reactive({ add: false, form: false });
const disable = reactive({ add: false });
const formRef = ref<FormInstance>();
const form = reactive<DataDictionary.ValueUpdate>({
   dic_id: "",
   value_id: "",
   name: "",
   status: "OPEN"
});

/** 表单规则 */
const formRules = reactive<FormRules>({
   name: [
      { required: true, message: "数据名称", trigger: "blur" },
      {
         min: 2,
         max: 20,
         message: "数据名称在 2 到 20 个字符之间",
         trigger: "change"
      },
      { pattern: /^[\u4E00-\u9FA5]+$/, message: "显示名称必须为中文", trigger: "change" }
   ],
   status: { required: true, message: "字典状态必填", trigger: "blur" }
});

async function handlerClick() {
   loading.add = true;
   disable.add = true;

   try {
      const res = await DataDictionaryApi.ValueUpdate(form);
      if (res.code == 200) {
         ElMessage.success("添加成功");
         dialogVisible.value = false;
      }
   } catch (error) {
      console.log("error", error);
   }

   loading.add = false;
   disable.add = false;
}

function handlerOpen() {
   loading.form = true;
   disable.add = true;
}

function handlerOpened() {
   if (!value.value) return;
   if (!dicId.value) return;

   form.dic_id = dicId.value;
   form.value_id = value.value.id;
   form.name = value.value.name;
   form.status = value.value.status;

   loading.form = false;
   disable.add = false;
}
</script>

<template>
   <el-dialog
      v-model="dialogVisible"
      ref="dialog"
      title="添加字典数据"
      width="500"
      draggable
      @open="handlerOpen"
      @opened="handlerOpened">
      <el-form v-loading="loading.form" ref="formRef" :model="form" :rules="formRules">
         <el-form-item label="数据名称:" prop="name">
            <el-input v-model="form.name" placeholder="数据名称" />
         </el-form-item>
         <el-form-item label="数据状态:" prop="status">
            <el-radio-group v-model="form.status">
               <el-radio label="OPEN">开启</el-radio>
               <el-radio label="CLOSE">关闭</el-radio>
            </el-radio-group>
         </el-form-item>
      </el-form>
      <template #footer>
         <el-button :loading="loading.add" :disabled="disable.add" type="primary" @click="handlerClick()"> 保存 </el-button>
      </template>
   </el-dialog>
</template>

<style lang="scss" scoped>
.el-input {
   width: 100%;
}
</style>
